﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;

namespace DAL
{
    public class DiemThiProvider
    {
        public static List<DiemThi> LayDanhSachDiemThi()
        {
            var lst = new List<DiemThi>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.DiemThis select u);

                foreach (var row in query)
                {
                    var dt = new DiemThi();
                    dt.DiemLyThuyet = row.DiemLyThuyet;
                    dt.DiemThucHanh = row.DiemThucHanh;
                    dt.KetQua = row.KetQua;
                    dt.MaKyThi = row.MaKyThi;
                    dt.MaHV = row.MaHV;
                    dt.MaKH = row.MaKH;
                    lst.Add(dt);
                }
            }

            return lst;
        }

        public static List<DiemThi> TimDanhSachDiem(object a, SoSanh ss)
        {
            var lst = new List<DiemThi>();
            using (var context = new QLTTTHEntities())
            {
                var query = (from u in context.DiemThis select u);

                foreach (var row in query)
                {
                    var dt = new DiemThi();
                    dt.DiemLyThuyet = row.DiemLyThuyet;
                    dt.DiemThucHanh = row.DiemThucHanh;
                    dt.KetQua = row.KetQua;
                    dt.MaKyThi = row.MaKyThi;
                    dt.MaHV = row.MaHV;
                    dt.MaKH = row.MaKH;

                    if (ss(a, dt) == 0)
                    {
                        lst.Add(dt);
                    }
                }
            }

            return lst;
        }


        public static int ThemDiemThi(DiemThi diemthi)
        {
            int maKH = -1;
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    context.DiemThis.Add(diemthi);
                    context.SaveChanges();
                    maKH = diemthi.MaKH;
                }
            }
            catch
            {

            }
            return maKH;
        }


        public static bool XoaDiemThi(int maKyThi, int maKH, int maHV)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.DiemThis
                                 where u.MaKH.Equals(maKH) && u.MaHV.Equals(maHV) && u.MaKyThi.Equals(maKyThi)
                                 select u).First();

                    context.DiemThis.Remove(query);
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }


        public static bool CapNhatDiemThi(DiemThi diemthi)
        {
            try
            {
                using (var context = new QLTTTHEntities())
                {
                    var query = (from u in context.DiemThis
                                 where u.MaKH.Equals(diemthi.MaKH) && u.MaHV.Equals(diemthi.MaHV) && u.MaKyThi.Equals(diemthi.MaKyThi)
                                 select u).First();

                    query.DiemLyThuyet = diemthi.DiemLyThuyet;
                    query.DiemThucHanh = diemthi.DiemThucHanh;
                    query.KetQua = diemthi.KetQua;
                    context.SaveChanges();
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

    }
}
